var http = require('http');
var fs = require('fs');
var url = require('url');
// 2.通过 http 模块创建服务对象
const server = http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  });
fs.open(__dirname + '/ReadMe.txt', 'r', (err, fd) => {
    //fd 是文件描述符。
    console.log("open: " + fd);
})
fs.readFile(__dirname + '/ReadMe.txt', 'utf8', (err, data) => {
    if (err) {
        console.error(err)
        return
    }
    console.log(data)
})
// 3.通过服务对象监听用户请求
server.on("request", (req, resp) => {
    let reqMethod = req.method.toLowerCase();
    console.log(11, __filename);
    // 请求路径
    let url = req.url;
    let path = url.split('?')[0];

    if (reqMethod === "get") {

        if (path === "/login") {
            console.log('get 处理登录请求', path);
        } else if (path === '/register') {
            console.log('get 处理注册请求', path);
        }

    } else if (reqMethod === "post") {

        if (path === "/login") {
            console.log('post 处理登录请求', path);
        } else if (path === '/register') {
            console.log('post 处理注册请求', path);
        }

    }


});
// 4.指定监听的端口号
server.listen(3000);
console.log('Server running at http://127.0.0.1:3000/');